

import sys
import json
import random


if __name__=='__main__':
    
    
    if len(sys.argv)<2:
        print sys.argv[0], '[cvis file]'
        exit()
    
    write_edges_probability=1.
    
    colors=[[215, 48, 39], [252, 141, 89], [254, 224, 144], 
            [224, 243, 248], [145, 191, 219], [0, 0, 0]]
    
    gene_color_dict={}
    
    for l in open('Data/CAIlistBins.csv'):
        s=l.split(',')
        #print s
        try:
            gene_color_dict[s[0]]=colors[int(s[1])]
            #print s[1], '...', int(s[1])
        except:
            print 'warning! bin is not integer:', l
            gene_color_dict[s[0]]=colors[int(float(s[1]))]

    
    print len(gene_color_dict)
        
    
    f=open('cvis_replaced_with_expression.gdf', 'w')
    
    for l in open(sys.argv[1]):
        s=l.split(',')
        if len(s)>3 and s[0]!='nodedef>name VARCHAR':
            #print s
            gene = s[1].split('-')[0]
            if gene in gene_color_dict:
                color= gene_color_dict[gene]
            else:
                color=colors[-1]
            #print color
            l2=','.join(s[:-3])
            l2+=',\''+str(color[0])+','+str(color[1])+','+str(color[2])+'\'\n'
            f.write(l2)
        elif s[0]=='nodedef>name VARCHAR' \
            or s[0]=='edgedef>node1 VARCHAR'\
            or random.uniform(0,1)<write_edges_probability:
            f.write(l)
    f.close()
    
